/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package jcphotorename_1_0.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.channels.FileChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author SoChishun
 * @version 1.0 2016-6-7 SoChishun Added.
 */
public class FileUtil {

    public static int n_file = 0, n_dir = 0, n_do = 0;
    public static long dirsize = 0;
    public static double dirsizemb = 0;

    /**
     * 复制文件
     *
     * @param file
     * @param destpath
     * @param is_digui
     * @param is_copy
     * @param rule
     * @param callback
     * @since 1.0 2016-6-7 sochishun Added.
     */
    public static void renameFile(File file, String destpath, boolean is_digui, boolean is_copy, String rule, ICallBack callback) {
        File[] files = file.listFiles();
        Date date = new Date();
        SimpleDateFormat sdf;
        String filepath, filename, ext, filesep = System.getProperty("file.separator");
        int dot;
        for (File file1 : files) {
            if (file1.isDirectory()) {
                if (is_digui) {
                    renameFile(file1, destpath, is_digui, is_copy, rule, callback);
                }
            } else {
                filepath = file1.getPath();
                dot = filepath.lastIndexOf('.');
                if (dot < 1) {
                    continue; // 不是有效文件,跳过
                }
                ext = filepath.substring(dot); // 扩展名(带点号)

                sdf = new SimpleDateFormat(rule);
                date.setTime(file1.lastModified());

                dot = filepath.lastIndexOf(filesep);
                // filename = filepath.substring(0, dot + 1) + sdf.format(date) + ext; // 就地重命名
                filename = destpath + filesep + sdf.format(date) + ext;
                System.out.println(filename);
                if (is_copy) {
                    try {
                        forTransfer(file1, new File(filename)); // 拷贝
                    } catch (Exception ex) {
                        Logger.getLogger(FileUtil.class.getName()).log(Level.SEVERE, null, ex);
                    }
                } else {
                    file1.renameTo(new File(filename)); // 移动
                }
                n_do++;
                callback.execute();
            }
        }
    }

    /**
     * 计算选定目录的文件个数
     *
     * @param file
     * @param is_digui
     * @since 1.0 2016-6-7 SoChishun Added.
     */
    public static void countFile(File file, boolean is_digui) {
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isDirectory()) {
                if (is_digui) {
                    countFile(file1, is_digui);
                }
                n_dir++;
            } else {
                dirsize += file1.length();
                n_file++;
            }
        }
    }

    /**
     * 复制文件
     *
     * @param f1
     * @param f2
     * @return
     * @throws Exception
     * @since 1.0 2016-6-7 sochishun added.
     * @see http://blog.sina.com.cn/s/blog_628d4dd10100lxmh.html
     */
    public static long forTransfer(File f1, File f2) throws Exception {
        long time = new Date().getTime();
        int length = 2097152;
        FileInputStream in = new FileInputStream(f1);
        FileOutputStream out = new FileOutputStream(f2);
        FileChannel inC = in.getChannel();
        FileChannel outC = out.getChannel();
        int i = 0;
        while (true) {
            if (inC.position() == inC.size()) {
                inC.close();
                outC.close();
                return new Date().getTime() - time;
            }
            if ((inC.size() - inC.position()) < 20971520) {
                length = (int) (inC.size() - inC.position());
            } else {
                length = 20971520;
            }
            inC.transferTo(inC.position(), length, outC);
            inC.position(inC.position() + length);
            i++;
        }
    }
}
